import { useState, useEffect } from 'react';
import moment from 'moment';
import { parseUserResume, getUserResume } from '@/services/user';

const useParseResume = ({ userCode, dictionary, form }) => {
    const [resumeData, setResumeData] = useState({});

    const resumeMap = {
        姓名: {
            key: 'employeeName',
            regex: [/^.{2,50}$/]
        },
        手机号: {
            key: 'mobile',
            regex: [/^\d{2,20}$/]
        },
        性别: {
            key: 'sex',
            dict: dictionary.sexType
        },
        最高学历: {
            key: 'highestDegree',
            dict: dictionary.agreeType
        },
        最近就职公司: {
            key: 'lastCompany',
            regex: [/^.{2,50}$/]
        },
        原就职职位: {
            key: 'lastPost',
            regex: [/^.{2,50}$/]
        },
        专业: {
            key: 'major',
            regex: [/^.{2,50}$/]
        },
        学校: {
            key: 'college',
            regex: [/^.{2,50}$/]
        },
        邮箱: {
            key: 'mail',
            regex: [/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/, /^.{2,50}$/]
        },
        出生日期: {
            key: 'birthdayDate',
            regex: [/^\d{4}-\d{2}-\d{2}$/]
        },
        出生时间: {
            key: 'birthdayTime',
            regex: [/^\d{4}-\d{2}-\d{2}$/]
        },
        毕业时间: {
            key: 'graduateTime',
            regex: [/^\d{4}-\d{2}$/]
        },
        证件号: {
            key: 'idCard',
            regex: [/^.{1,50}$/]
        },
        邮编: {
            key: 'postCode',
            regex: [/^.{1,50}$/]
        }
    };

    const fetchResumeData = async fileName => {
        let talentFileName;
        if (fileName) {
            talentFileName = fileName;
        } else {
            const resumeResult = await getUserResume({ userCode });
            if (resumeResult.code !== 10000) return;

            const {
                data: { talentFile }
            } = resumeResult;
            if (!talentFile || !talentFile.length) return;
            const { originName } = talentFile[0];
            talentFileName = originName;
        }
        // const originName = '架构师java--彭敏.docx';
        const parsedResumeResult = await parseUserResume({ talentFileName });
        if (parsedResumeResult.code !== 10000) return;
        const { data } = parsedResumeResult;
        const obj = {};
        Object.keys(resumeMap).forEach(mapKey => {
            const mapData = resumeMap[mapKey];
            if (!data[mapKey]) return;
            if (mapData.regex) {
                if (Array.isArray(mapData.regex) && mapData.regex.every(item => item.test(data[mapKey]))) {
                    obj[mapData.key] = data[mapKey];
                }
            }
            if (mapData.dict) {
                const dictObj = mapData.dict.find(item => item.dictName === data[mapKey]);
                if (dictObj) obj[mapData.key] = dictObj.dictValue;
            }
        });
        setResumeData({
            ...obj
        });
    };

    useEffect(() => {
        if (userCode) {
            fetchResumeData();
        }
    }, [userCode, dictionary]);

    const setFormFields = fieldMap => {
        const fieldValue = {};
        const timeTypeKeys = ['birthdayDate', 'birthdayTime', 'graduateTime'];
        Object.keys(resumeData).forEach(key => {
            if (fieldMap[key]) {
                const value = resumeData[key];
                fieldValue[fieldMap[key]] = timeTypeKeys.includes(key) ? moment(value) : value;
            }
        });
        form.setFieldsValue(fieldValue);
    };

    return {
        resumeData,
        setFormFields,
        fetchResumeData
    };
};

export default useParseResume;
